home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / hlvector.lha / hl_vector / vhjmin.cc < prev    next >
C/C++ Source or Header  |  1993-08-08  |  5KB  |  202 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /*
  3.  ************************************************************************
  4.  *
  5.  *           Verify the Hook-Jeevse minimization
  6.  *
  7.  ************************************************************************
  8.  */
  9.  
  10. #include "LinAlg.h"
  11. #include "math_num.h"
  12. #include <builtin.h>
  13. #include <ostream.h>
  14.  
  15.  
  16. static int iter_count;
  17.  
  18. /*
  19.  *-----------------------------------------------------------------------
  20.  *             Simplified vector print
  21.  */
  22.  
  23. static void pr_vector(Vector& v)
  24. {
  25.   register int i;
  26.   for(i=v.q_lwb(); i<=v.q_upb(); i++)
  27.      cout << form("%9.3g  ",v(i));
  28. }
  29.  
  30.  
  31. /*
  32.  *------------------------------------------------------------------------
  33.  *              Rosenbroke function
  34.  */
  35.  
  36. double f1(const Vector& v)
  37. {
  38.   register double x1 = v(1);
  39.   register double x2 = v(2);
  40.   iter_count++;
  41.   return 100*sqr(x2 - x1*x1) + sqr(1 - x1);
  42. }
  43.  
  44. void test1()
  45. {
  46.   const int n = 2;
  47.   Vector b0(1,n,-1.2,1.,"END");            // Initial guess
  48.   Vector b(b0);                    // Min location found
  49.   Vector bm(1,n,1.0,1.0,"END");            // Exact min location
  50.   Vector h0(1,n,10.,10.,"END");            // Initial step
  51.  
  52.   b=b0;
  53.   iter_count = 0;
  54.   cout << "\n\n\tRosenbroke function\n";
  55.   cout << "\n\n\tf = 100*(x2-x1^2)^2 + (1-x1)^2\n\n";
  56.   cout << "\nInitial guess         b0 = "; pr_vector(b0);
  57.   cout << "\nFunction value at it  f0 = " << f1(b0);
  58.   cout << "\nInitial steps         h0 = "; pr_vector(h0);
  59.   cout << "\n";
  60.   cout << "\nMinimum f value found f  = " << hjmin(b,h0,f1);
  61.   cout << "\n                  at  b  = "; pr_vector(b);
  62.   cout << "\nExact min location    bm = "; pr_vector(bm);
  63.   cout << "\nNo. of iterations     ni = " << iter_count << "\n";
  64.  
  65. }
  66.  
  67.  
  68.  
  69. /*
  70.  *------------------------------------------------------------------------
  71.  *                 Bocks function
  72.  */
  73.  
  74. double f2(const Vector& v)
  75. {
  76.   register double x1 = v(1);
  77.   register double x2 = v(2);
  78.   iter_count++;
  79.   return sqr( exp(-1./10) - exp(-x1/10) + exp(-10./10) - exp(-x2/10) );
  80. }
  81.  
  82. void test2()
  83. {
  84.   const int n = 2;
  85.   Vector b0(1,n,0.,0.,"END");            // Initial guess
  86.   Vector b(b0);                    // Min location found
  87.   Vector bm(1,n,10.0,1.0,"END");        // Exact min location
  88.   Vector h0(1,n,10.,10.,"END");            // Initial step
  89.  
  90.   b = b0;
  91.   iter_count = 0;
  92.   cout << "\n\n\tBocks function\n";
  93.   cout << "\n\n\t"
  94.       "f = [ exp(-1/10) - exp(-x1/10) + exp(-10/10) -exp(-x2/10) ]^2\n\n";
  95.   cout << "\nInitial guess         b0 = "; pr_vector(b0);
  96.   cout << "\nFunction value at it  f0 = " << f2(b0);
  97.   cout << "\nInitial steps         h0 = "; pr_vector(h0);
  98.   cout << "\n";
  99.   cout << "\nMinimum f value found f  = " << hjmin(b,h0,f2);
  100.   cout << "\n                  at  b  = "; pr_vector(b);
  101.   cout << "\nExact min location    bm = "; pr_vector(bm);
  102.   cout << "\nNo. of iterations     ni = " << iter_count << "\n";
  103.  
  104. }
  105.  
  106.  
  107.  
  108. /*
  109.  *------------------------------------------------------------------------
  110.  *            Mile & Cuntrell function
  111.  */
  112.  
  113. double f3(const Vector& v)
  114. {
  115.   register double x1 = v(1);
  116.   register double x2 = v(2);
  117.   register double x3 = v(3);
  118.   register double x4 = v(4);
  119.   iter_count++;
  120.   return pow( exp(x1)-x2, 4) + 100*pow(x2-x3,6) + pow(atan(x3-x4),4) +
  121.      pow(x1,8);
  122. }
  123.  
  124. void test3()
  125. {
  126.   const int n = 4;
  127.   Vector b0(1,n,1.,2.,2.,2.,"END");        // Initial guess
  128.   Vector b(b0);                    // Min location found
  129.   Vector bm(1,n,0.,1.,1.,1.,"END");        // Exact min location
  130.   Vector h0(1,n,10.,10.,10.,10.,"END");        // Initial step
  131.  
  132.   b = b0;
  133.   iter_count = 0;
  134.   cout << "\n\n\tMile & Cuntrell function\n";
  135.   cout << "\n\n\t"
  136.       "f = [ exp(x1)-x2 ]^4 +100(x2-x3)^6 + atan(x3-x4)^4 + x1^8\n\n";
  137.   cout << "\nInitial guess         b0 = "; pr_vector(b0);
  138.   cout << "\nFunction value at it  f0 = " << f3(b0);
  139.   cout << "\nInitial steps         h0 = "; pr_vector(h0);
  140.   cout << "\n";
  141.   cout << "\nMinimum f value found f  = " << hjmin(b,h0,f3);
  142.   cout << "\n                  at  b  = "; pr_vector(b);
  143.   cout << "\nExact min location    bm = "; pr_vector(bm);
  144.   cout << "\nNo. of iterations     ni = " << iter_count << "\n";
  145.  
  146. }
  147.  
  148. /*
  149.  *------------------------------------------------------------------------
  150.  *              Powell function
  151.  */
  152.  
  153. double f4(const Vector& v)
  154. {
  155.   register double x1 = v(1);
  156.   register double x2 = v(2);
  157.   register double x3 = v(3);
  158.   register double x4 = v(4);
  159.   iter_count++;
  160.   return sqr(x1+10*x2) + 5*sqr(x3-x4) + pow(x2-2*x3,4) + 10*pow(x1-x4,4);
  161. }
  162.  
  163. void test4()
  164. {
  165.   const int n = 4;
  166.   Vector b0(1,n,3.,-1.,0.,1.,"END");        // Initial guess
  167.   Vector b(b0);                    // Min location found
  168.   Vector bm(1,n,0.,0.,0.,0.,"END");        // Exact min location
  169.   Vector h0(1,n,10.,10.,10.,10.,"END");        // Initial step
  170.  
  171.   b = b0;
  172.   iter_count = 0;
  173.   cout << "\n\n\tPowell function\n";
  174.   cout << "\n\n\t" 
  175.       "f = (x1+10*x2)^2 + 5(x3-x4)^2 + (x2-2x3)^4 + 10(x1-x4)^4\n\n";
  176.   cout << "\nInitial guess         b0 = "; pr_vector(b0);
  177.   cout << "\nFunction value at it  f0 = " << f4(b0);
  178.   cout << "\nInitial steps         h0 = "; pr_vector(h0);
  179.   cout << "\n";
  180.   cout << "\nMinimum f value found f  = " << hjmin(b,h0,f4);
  181.   cout << "\n                  at  b  = "; pr_vector(b);
  182.   cout << "\nExact min location    bm = "; pr_vector(bm);
  183.   cout << "\nNo. of iterations     ni = " << iter_count << "\n";
  184.  
  185. }
  186.  
  187.  
  188. /*
  189.  *------------------------------------------------------------------------
  190.  *                Root module
  191.  */
  192.  
  193. main()
  194. {
  195.   cout << "\n\n\n\t\tVerify HJMIN multidimensional minimizer\n";
  196.   test1();
  197.   test2();
  198.   test3();
  199.   test4();
  200. }
  201.  
  202.